"""
    TorrentBrowser plugin for XBMC
    Copyright (C) 2011-2012 Markus Langenoja
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Function [TBScrape]
    -------------------
    * scrapeInfo() - Returns datamined information for each of the items obtained from
      TBBrowse.getItems()
    * getID(title) - Returns IMDB-ID from a given string, title, this being a release
      entry on the torrent site.
    * sortInfo() - Every torrent site has a unique set of parameter position for 
      torrent-id, title and imdb-id, this sorts the given info to be unison.
    * scrapePage(page) - Given an themoviedbAPI-url it returns information in form of a dict.
"""
import string, re, sys
import TBTools

class TBScraper( TBTools.TBTools ):
    __deb__ = sys.modules[ "__main__" ].__deb__
    __plugin__ = sys.modules[ "__main__" ].__plugin__
    __web__ = sys.modules[ "__main__" ].__web__
    __settings__ = sys.modules[ "__main__" ].__settings__

    TMDBAPI = 'http://api.themoviedb.org/2.1/Movie.imdbLookup/en/xml/6b6effafe7c0b6fa17191d0430f546f8/tt'
    IMDBAPI = 'http://www.imdbapi.com/?t='

    def scrapeInfo( self, item ):
        get = item.get

        # Check if IMDB-id is given else find it
        if get( 'imdb' ) != None:
            if self.__deb__:
                print self.__plugin__ + '::DEBUG::TBScrape::' + 'IMDB-ID Given: tt' + get( 'imdb' )
            # If IMDB-id exists
            query = self.TMDBAPI + get( 'imdb' )
        else:
            if self.__deb__:
                print self.__plugin__ + '::DEBUG::TBScrape::' + 'IMDB-ID NOT Given, Fetching'
            # If not, clean up title from scraped info and fetch imdb-ID

            originaltitle = get( 'title' )
            title = self.replaceBadSigns( get( 'title' ) )
            # Here the release name is stripped!
            strippedtitle = self.stripTitle( title )

            item['imdb'] = self.getIMDBID( strippedtitle )

            if get( 'imdb' ):
                if self.__deb__:
                    print self.__plugin__ + '::DEBUG::TBScrape::' + 'Fetched IMDB-ID: tt' + get( 'imdb' )
                query = self.TMDBAPI + get( 'imdb' )
            else:
                if self.__deb__:
                    print self.__plugin__ + '::DEBUG::TBScrape::' + 'Failed to retrieve IMDB-ID'
                query = False

        # If a valid query is return from statements above, get page and
        # scrape info into infoList
        if query:
            if self.__deb__:
                print self.__plugin__ + '::DEBUG::TBScrape::' + 'query for TMDB: ' + query
            try:
                item['tmdbdata'] = self.__web__.webOpen( query )
                check = item['title']
                item = self.scrapeTMDB( item )
            except:
                if self.__deb__:
                    print self.__plugin__, '::ERROR::scrapeInfo::Could not open:', query

        if self.__settings__.getSetting( 'original' ):
            item['title'] = originaltitle
        elif check != item['title']:
            if type( title ) == tuple:
                item['title'] = title[0]
            else:
                item['title'] = title

        self.item = item
        return self.item

    def getIMDBID( self, data ):
        if self.__deb__:
            print self.__plugin__ + '::DEBUG::TBScrape::' + 'Fetching IMDB-ID with: '
            print data
        # Searches the iMDB-API using Title.

        # Create the query, searches with year if given.
        if type( data ) == tuple:
            info = string.replace( data[0], ' ', '%20' ) + '&y=' + data[1]
            title = data[0]
        else:
            info = string.replace( data, ' ', '%20' )
            title = data
        query = self.IMDBAPI + info
        page = self.__web__.webOpen( query )
        if re.match( '\{"Response":".*', page ):
            # If bad response, give no imdbid back.
            if self.__deb__:
                print self.__plugin__, '::ERROR::getID::Response: Parse Error for:', title
            imdbid = None
        else:
            # Else start by splitting the title from both the input AND the title obtained
            # using the iMDB-API and sort the words by size, starting with the longest
            # Also strips the API-title since it can contain characters stripped when
            # cleaning title.
            compInput = sorted( title.split(), key=len, reverse=True )
            apiTitle = re.compile( '"Title":"(.+?)"' ).findall( page )[0]
            apiTitle = self.stripTitle( apiTitle )
            compAPI = sorted( apiTitle.split(), key=len, reverse=True )

            if self.__deb__:
                print self.__plugin__, '::DEBUG::getID::Comparing : compInput:', compInput, ' :!: compAPI:', compAPI

            try:
                # Comparisson between title and title from iMDB-API.
                # Only returns a true hit if the two longest word in the title matches.
                # Crashes if title concist of only one word.
                if compInput[0].lower() == compAPI[0].lower():
                    # Compares the longest word in the titles
                    if compInput[1].lower() == compAPI[1].lower():
                        # Compares the second longest word in titles.
                        ok = True
                    else:
                        if self.__deb__:
                            print self.__plugin__, '::DEBUG::getID::Comparisson 2 failed for :', title
                        ok = False
                else:
                    if self.__deb__:
                        print self.__plugin__, '::DEBUG::getID::Comparisson 1 failed for :', title
                    ok = False
            except:
                # If list is just one entry, compare this and return ok if all good.
                if compInput[0].lower() == compAPI[0].lower():
                    ok = True
                else:
                    if self.__deb__:
                        print self.__plugin__, '::DEBUG::getID::Comparisson failed for :', title
                    ok = False

            # If the comparissons above return an ok = True, then fetch imdbid finally.       
            if ok:
                imdbid = re.compile( '"ID":"tt(.+?)"' ).findall( page )[0]
                if self.__deb__:
                    print self.__plugin__, '::DEBUG::getID::Match Successful for ID: tt' + str( imdbid )
            else:
                imdbid = None
        return imdbid

    def scrapeTMDB( self, item ):
        get = item.get
        page = get( 'tmdbdata' )
        item.pop( 'tmdbdata' )

        if self.__deb__:
            print self.__plugin__ + '::DEBUG::TBScrape::' + 'Running scrapeTMDB for: ' + get( 'title' )
        # Initiate info dictionary

        # Extract wanted information from TheMovieDB, if it doesn't exist it doesn't write anything to the dictionary.
        try:
            item['title'] = self.replaceBadSigns( re.compile( '<name>(.+?)</name>' ).findall( page )[0] )
        except: pass
        try:
            item['plot'] = self.replaceBadSigns( re.compile( '<overview>(.+?)</overview>' ).findall( page )[0] )
        except: pass
        try:
            item['duration'] = re.compile( '<runtime>(.+?)</runtime>' ).findall( page )[0]
        except: pass
        try:
            item['mpaa'] = re.compile( '<certification>(.+?)</certification>' ).findall( page )[0]
        except: pass
        try:
            item['rating'] = float( re.compile( '<rating>(.+?)</rating>' ).findall( page )[0] )
        except: pass
        try:
            genre = re.compile( '<category type="genre" name="(.+?)"' ).findall( page )
            item['genre'] = string.join( genre, '/' )
        except: pass
        try:
            item['poster'] = re.compile( '<image type="poster" url="(.+?)" size="original"' ).findall( page )[0]
        except: pass
        try:
            item['backdrop'] = re.compile( '<image type="backdrop" url="(.+?)" size="original"' ).findall( page )[0]
        except: pass
        try:
            item['premiered'] = re.compile( '<released>(.+?)</released>' ).findall( page )[0][0:4]
        except: pass

        # Return list
        return item
